package com.jiutong.meipenggang.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.orhanobut.logger.Logger;


/**
 * @author Veken
 * @date on 2017/12/1 14:07
 * @describe
 */

public class TextChangeColorView extends TextView {

    private Paint mOrginalPaint;
    private Paint mChangePaint;
    private int mOrginalColor;
    private int mChangeColor;


    //当前进度
    private float mCurrentProgress;

    private Direction mDirection = Direction.LEFT_TO_RIGHT;


    public enum Direction {
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }

    public void setmDirection(Direction mDirection) {
        this.mDirection = mDirection;
    }

    public void setmCurrentProgress(float mCurrentProgress) {
        this.mCurrentProgress = mCurrentProgress;
        invalidate();
    }

    public void setChangeColor(int changeColor) {
        this.mChangePaint.setColor(changeColor);
    }

    public void setOrginalColor(int orginalColor) {
        this.mOrginalPaint.setColor(orginalColor);
    }


    public TextChangeColorView(Context context) {
        this(context, null);
    }

    public TextChangeColorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextChangeColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextChangeColorView);
        mOrginalColor = typedArray.getColor(R.styleable.TextChangeColorView_orginalColor, getResources().getColor(R.color.hongbao_table_gray));
        mChangeColor = typedArray.getColor(R.styleable.TextChangeColorView_changeColor, getResources().getColor(R.color.gesture_color));

        mOrginalPaint = getPaintByColor(mOrginalColor);
        mChangePaint = getPaintByColor(mChangeColor);

        typedArray.recycle();
    }

    /**
     * 根据颜色获取画笔
     */
    private Paint getPaintByColor(int color) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setTextSize(getTextSize());
        return paint;
    }

    /**
     * 利用canvas.clipRect()的API可以裁剪，左边用一个画笔去画，右边用一个画笔去画，不断的改变中间值
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        //自己来画
        //根据进度获取中间值
        int middle = (int) (mCurrentProgress * getWidth());
        //从左变到右  左边是蓝色，右边是黑色
        if (mDirection == Direction.LEFT_TO_RIGHT) {
            drawText(canvas, mChangePaint, 0, middle);
            //绘制变色
            drawText(canvas, mOrginalPaint, middle, getWidth());
        } else {
            //从右到右左  左边是黑色，右边是蓝色
            drawText(canvas, mChangePaint, getWidth() - middle, getWidth());
            //绘制变色
            drawText(canvas, mOrginalPaint, 0, getWidth() - middle);
        }

    }

    /**
     * 绘制Text
     *
     * @param canvas
     * @param paint
     * @param start
     * @param end
     */
    private void drawText(Canvas canvas, Paint paint, int start, int end) {
        //必须先保存画布save，画完以后要释放restore，不然不显示
        canvas.save();
        //canvas.clipRect()  裁剪区域
        //绘制不变色的区域 ，先裁剪再画，或者先画再裁剪都可以
        Rect rect = new Rect(start, 0, end, getHeight());
        canvas.clipRect(rect);
        String text = getText().toString();
        //用画笔测量文字宽度
//        int x = (int) (getWidth()/2-mOrginalPaint.measureText(text)/2);
        //用TestBounds来测量
        Rect bounds = new Rect();
        //获取字体的宽度
        paint.getTextBounds(text, 0, text.length(), bounds);
        int dx = getWidth() / 2 - bounds.width() / 2;
        //基线baseLine
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        //这么画其实还是只有一种颜色
        canvas.drawText(text, dx, baseLine, paint);
        canvas.restore();

    }
}
